package com.example.boot.controller;

import com.example.boot.oss.UploadFile;
import com.example.boot.pojo.MxGroupEntity;
import com.example.boot.service.MxGroupService;
import com.example.boot.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Map;



/**
 * 
 *
 * @author yy
 * @email yangxxil01@sina.com
 * @date 2022-01-08 14:35:28
 */
@RestController
@RequestMapping("mxgroup")
public class MxGroupController {
    @Autowired
    private MxGroupService mxGroupService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        return R.ok(mxGroupService.list());

    }


    /**
     * 信息
     */
    @RequestMapping("/info/{groupId}")
    public R info(@PathVariable("groupId") Long groupId){
		MxGroupEntity mxGroup = mxGroupService.getById(groupId);

        return R.ok(mxGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MxGroupEntity mxGroup){
		mxGroupService.save(mxGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MxGroupEntity mxGroup){
		mxGroupService.updateById(mxGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody MxGroupEntity mxGroup){
		mxGroupService.removeByIds(Arrays.asList(mxGroup.getGroupId()));

        return R.ok();
    }
    /**
     * @Description: 上传用户头像
     */
    @RequestMapping("/uploadImg")
    public R uploadImg(@RequestParam Long groupId, @RequestParam(value = "imgFile", required = false) MultipartFile file) {


        //获取上传文件输入流
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取文件名称
        String fileName = file.getOriginalFilename();
        String faceImage=  UploadFile.uploadFileAvatar(inputStream,fileName);




        // 更细用户头像
        MxGroupEntity user = new MxGroupEntity();
        user.setGroupId(groupId);
        user.setFaceImage(faceImage);


        mxGroupService.updateById(user);

        return R.ok();
    }
}
